/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Mr.Nattasan Wang-Aphisit
 * @version 1.1
 */

package sandbox;

import wads.drawing.node.CaseNode;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;

@Deprecated
public class CaseNodeDialog extends JDialog {
//  private static final long serialVersionUID = 1134893170646496325L;
  
  private JTabbedPane tabbedPane;
  private JPanel panels[];
  private JTextPane textPanes[];
  private CaseNode node;
  private int panelCount=0;
  private int returnValue;
  private String panelNameList[];
//  private Font labelFont = new Font("CordiaUPC", Font.PLAIN, 17);
  
  public CaseNodeDialog(CaseNode node, String[] panelNames){
    super();
//    this.setFont(labelFont);
    this.setModal(true);
    this.node = node;
    if (node == null)
      return;
    
    panelNameList = panelNames;
    
    //Set number of panels to create
    panelCount = panelNames.length;
    
    if (panelCount <= 0)
      return;

    //Init panel and text panes
    panels = new JPanel[panelCount];
    
    textPanes = new JTextPane[panelCount];
    //Add a tabbed pane to dialog
    tabbedPane = new JTabbedPane();
    this.getContentPane().add(tabbedPane,"Center");

    //Add buttons
    JPanel buttonsPanel = new JPanel();
    JButton okButton = new JButton("Ok");
    JButton cancelButton = new JButton("Cancel");
    okButton.addActionListener(new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent evt){
        returnValue = JOptionPane.OK_OPTION;
        CaseNodeDialog.this.setVisible(false);
        
        //Note : An err will occur if this line is used instead :
        //-> node.dialogClosing(returnValue);
        //It will say that it needs node to be final
        CaseNodeDialog.this.node.closeDialog(returnValue);
      }
    });

    cancelButton.addActionListener(new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent evt){
        returnValue = JOptionPane.CANCEL_OPTION;
        CaseNodeDialog.this.setVisible(false);
      }
    });

    buttonsPanel.add(okButton);
    buttonsPanel.add(cancelButton);
    this.getContentPane().add(buttonsPanel,"South");

    //Add some panes to the tabbed pane
    for (int i=0;i<panelNames.length;i++){
      panels[i] = addDefaultPane(i,panelNames[i]);
    }

    tabbedPane.setSelectedIndex(0);
    this.setSize(350,250);
    setResizable(false);
    this.setModal(true);
  }

  private JPanel addDefaultPane(int index, String panelName){
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
//    panel.setFont(labelFont);
    this.tabbedPane.addTab(panelName,panel);
//    this.tabbedPane.setFont(labelFont);
    
    JScrollPane scrollPane = new JScrollPane();
    textPanes[index] = new JTextPane();
//    textPanes[index].setFont(labelFont);
    scrollPane.getViewport().add(textPanes[index],"Center");
    textPanes[index].setSize(300,200);
    
    panel.add(scrollPane);
    
    return panel;
  }

  public int getContentCount(){
    //Return the number of text pane in this dialog
    return panelCount;
  }

  public String getTextPaneContent(int index){
    if (index+1 > panelCount){
      System.out.println("Error: CaseNodeDialog.getTextPanelContent: Index out of range");
      return null;
    }

    return new String(textPanes[index].getText());
  }

  public boolean setTextPaneContent(int index, String text){
    if (index+1 > panelCount){
      System.out.println("Error: CaseNodeDialog.setTextPanelContent: Index out of range");
      return false;
    }

    if (text== null)
      text = "";

    //Set the text pane with given text
    textPanes[index].setText(text);

    return true;
  }

  // WongVW Add
  public String getPanelName(int index){
    if (index+1 > panelCount){
      System.out.println("Error: CaseNodeDialog.getTextPanelContent: Index out of range");
      return null;
    }

    return new String(panelNameList[index]);
  }
}
